package com.jinzhi.jzsynthesize.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.sys.domain.UserDO;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("/synthesize/printOutput")
public class ExamController extends BaseController {

	@Autowired
	private StuSignUpService stuSignUpService;
	@Autowired
	private CertificationBatchService certificationBatchService;
	@Autowired
	private ExaminationBatchService examinationBatchService;


	@GetMapping()
	@RequiresPermissions("synthesizeWeb:printOutput:printOutput")
	String print (){
		return "synthesizeWeb/printOutput/printOutput";
	}

	@Log("打印输出页面数据查询-分页")
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("synthesizeWeb:printOutput:printOutput")
	public Result<Page<StuSignUpDO>> list(HttpServletRequest req,StuSignUpDO stuSignUpDO){
		UserDO user = this.getUser();

		Long cBatchId = examinationBatchService.findByCbatchId(stuSignUpDO.getEBatchId());

		//查询用户角色
		String roleName = getRoleNames();
		Page<StuSignUpDO> page = null;
		if (roleName.indexOf("超级用户角色") == -1) {
			Object orgId = req.getSession().getAttribute("orgId");
			if (orgId != null){
				page = stuSignUpService.selectPage(getPage(StuSignUpDO.class),
						stuSignUpService.convertToEntityWrapper("state",3 ,"org_id",orgId,
								"cbatch_id" , cBatchId , "eallocation_num" , stuSignUpDO.getEallocationNum())
								.orderBy("id", false));
			}else {
				page = stuSignUpService.selectPage(getPage(StuSignUpDO.class),
						stuSignUpService.convertToEntityWrapper("state",3 ,"org_id",user.getOrgId(),
								"cbatch_id" , cBatchId , "eallocation_num" , stuSignUpDO.getEallocationNum())
								.orderBy("id", false));
			}

		}else {
			page = stuSignUpService.selectPage(getPage(StuSignUpDO.class),
					stuSignUpService.convertToEntityWrapper("state",3 ,
							"cbatch_id" , cBatchId , "eallocation_num" , stuSignUpDO.getEallocationNum())
							.orderBy("id", false));
		}


			List<StuSignUpDO> stuList = page.getRecords();
		for (StuSignUpDO signUp :stuList){
//			ExaminationAllocationDO examAll = examinationAllocationService.selectById(signUp.getEallocationId());
			String seatNum = signUp.getTicketNumber().substring(signUp.getTicketNumber().length() -2,signUp.getTicketNumber().length());
			signUp.setSeatNum(seatNum);

			//认证批次信息
			CertificationBatchDO cBatch = certificationBatchService.selectById(signUp.getCbatchId());
			//查询考场信息
			if (cBatch != null){
				signUp.setCbatchName(cBatch.getName());
			}
		}
		page.setRecords(stuList);
		return Result.ok(page);
	}

	@Log("打印输出页面数据查询-无分页")
	@ResponseBody
	@GetMapping("/printList")
	@RequiresPermissions("synthesizeWeb:printOutput:printOutput")
	public Result<List<StuSignUpDO>> print(HttpServletRequest req,StuSignUpDO stuSignUpDO){
		UserDO user = this.getUser();

		Long cBatchId = examinationBatchService.findByCbatchId(stuSignUpDO.getEBatchId());

		Object orgId = req.getSession().getAttribute("orgId");
		Wrapper<StuSignUpDO> wrapper = new EntityWrapper<StuSignUpDO>().orderBy("id" , false);
		wrapper.eq("state" , 3);
		if (orgId != null){
			wrapper.eq("org_id" , orgId);
		}else {
			wrapper.eq("org_id" , user.getOrgId());
		}
		wrapper.eq("cbatch_id" , cBatchId);
		wrapper.eq("eallocation_num", stuSignUpDO.getEallocationNum());

		List<StuSignUpDO> stuList = stuSignUpService.selectList(wrapper);
		for (StuSignUpDO signUp :stuList){
//			ExaminationAllocationDO examAll = examinationAllocationService.selectById(signUp.getEallocationId());
			//认证批次信息
			CertificationBatchDO cBatch = certificationBatchService.selectById(signUp.getCbatchId());
			//查询考场信息
			if (cBatch != null){
				signUp.setCbatchName(cBatch.getName());
			}
		}
		return Result.ok(stuList);
	}


	@Log("查询认证批次列表-无分页")
	@ResponseBody
	@GetMapping("/cBatch")
	Result<?> findCBatchList(){
		List<CertificationBatchDO> cBatchList =certificationBatchService.findList();

		return Result.ok(cBatchList);
	}

	@Log("查询考试批次列表-无分页")
	@ResponseBody
	@GetMapping("/eBatch")
	Result<?> findEBatchList(){
		Wrapper<ExaminationBatchDO> wrapper = new EntityWrapper<>();
		/*wrapper.ne("state" , 0);
		wrapper.ne("state" , 2);
		wrapper.ne("state" , 3);
		wrapper.ne("state" , 4);
		wrapper.ne("state" , 5);
		wrapper.ne("state" , 6);
		wrapper.ne("state" , 9);*/
		wrapper.eq("state" , 6);
		List<ExaminationBatchDO> eBatchList = examinationBatchService.selectList(wrapper);

		return Result.ok(eBatchList);
	}

	@Log("跳转打印页面")
	@GetMapping("/print")
	String print(Model model,String eBatchId, String eallocationNum) {

		ExaminationBatchDO exam = examinationBatchService.selectById(eBatchId);

		model.addAttribute("examName" , exam.getName());
		model.addAttribute("eBatchId", eBatchId);
		model.addAttribute("eallocationNum", eallocationNum);
		return "synthesizeWeb/printOutput/info";
	}
}
